<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- default-expand-all默认展开所有的子节点 -->
        <el-tree
          default-expand-all
          :data="list"
        ><template #default="{data}">
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>

        </template>
        </el-tree>

      </el-card>
      <!-- 添加弹框
       :close-on-click-modal="false" 关闭在遮罩层处点击关闭弹框的功能
       :close-on-press-escape="false" 关闭按esc键关闭弹窗功能
       -->
      <el-dialog
        :title="isEdit?'编辑部门':'新增部门'"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        @close="hClose"
      >
        <AddorEdit
          v-if="showDialog"
          :id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="off"
        />
      </el-dialog>

    </div>
  </div>
</template>
<script>
import AddorEdit from './deptDialog.vue'
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      curId: '', // 设置一个字符串 用来父传子将id传给子元素
      list: [],
      showDialog: false, // 控制弹层的显示隐藏
      isEdit: false, // 编辑弹层的显示喝隐藏 false新增 true编辑
      originList: []
    }
  },
  created() {
    this.dataList()
  },
  methods: {
    // 重置表单，清空校验信息
    hClose() {
      this.$refs.myAddRef.resetForm()
    },
    // 删除部门
    hDel(id) {
      // 询问是否要删除
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(
        async() => {
          const res = await delDepartments(id)
          console.log('删除', res)
          if (res.code !== 10000) return this.$message.error(res.message)
          this.$message.success(res.message)
        }).catch(error => error)
      // 重新渲染页面
      this.dataList()
    },
    // 点击取消关闭弹传
    off() {
      this.showDialog = false
    },
    // 添加编辑弹传
    hEdit(id) {
      this.isEdit = true
      this.curId = id
      this.showDialog = true
    },
    // 重新渲染 关闭弹层
    hSuccess() {
      this.dataList()
      // 关闭弹层
      this.showDialog = false
    },
    hAdd(id) {
      this.isEdit = false
      this.curId = id
      this.showDialog = true
    },
    // 渲染数据发送axios
    async dataList() {
      try {
        const res = await getDepartments()
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        res.data.depts.shift()
        this.list = transDataToTreeData(res.data.depts)
        console.log('用户信息', res)
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>
